package com.example.administrator.myapplication.adapter;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.mode.InformationDataManager;
import com.example.administrator.myapplication.mode.InformationListData;
import com.example.administrator.myapplication.view.other.AgreementActivity;

import java.util.ArrayList;

/**
 * 说明：记录的适配器
 * 作者： 阳2012; 创建于： 2017/5/6 8:52
 */
public class InformationAdapter extends RecyclerView.Adapter<InformationAdapter.MyViewHolder> {
    ArrayList<InformationListData> mDatas;
    Context mContext;

    public InformationAdapter(Context context, ArrayList<InformationListData> datas) {
        mDatas = datas;
        mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_intformation, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        Glide
                .with(mContext)
                .load(mDatas.get(position).getImgPath())
                .into(holder.icon);


        holder.content.setText(mDatas.get(position).getContent());
        holder.time.setText(mDatas.get(position).getContent() + "\n" + mDatas.get(position).getTime());

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaterialDialog materialDialog = new MaterialDialog.Builder(mContext)
                        .title("提示")
                        .positiveText("确定")
                        .negativeColorRes(R.color.gray)
                        .negativeText("取消")
                        .content("确定要删除该记录吗？")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                InformationDataManager.getInstands().deleteData(mDatas.get(position));
                            }
                        })
                        .build();
                materialDialog.show();
            }
        });

        holder.loock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AgreementActivity.startUI(mContext,mDatas.get(position).getUrl());

            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView content;
        TextView time;
        ImageView delete;
        TextView loock;

        public MyViewHolder(View view) {
            super(view);
            icon = (ImageView) view.findViewById(R.id.picture);
            content = (TextView) view.findViewById(R.id.content);
            time = (TextView) view.findViewById(R.id.time);
            delete = (ImageView) view.findViewById(R.id.delete);
            loock = (TextView) view.findViewById(R.id.loock);
        }
    }
}
